package com.nine.shop.controller.admin;

import com.nine.shop.common.ServerResponse;
import com.nine.shop.controller.BaseController;
import com.nine.shop.model.Admin;
import com.nine.shop.model.User;
import com.nine.shop.service.AdminService;
import com.nine.shop.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther: hty
 * @Date: 2019/8/25 20:37
 * @Description:后台产品管理
 */
@RestController
@RequestMapping("api/shopping/admin/users")
public class AdminAdmin extends BaseController {
    @Resource
    private AdminService adminService;
    @Resource
    private UserService userService;

    @CrossOrigin
    @RequestMapping(value = "searchallusers.do",method = RequestMethod.POST)
     public ServerResponse SearchAllUsers(@RequestParam(name = "current", defaultValue = "1") Integer current,
                                          @RequestParam(name = "size", defaultValue = "5") Integer size) {
        int id = (int) super.getSession().getAttribute("admininfo");
        // int id = 1;
        Admin admin =new Admin();
        admin = adminService.getById(id);
        if (admin.getId() == null) return ServerResponse.createByErrorMsg("该管理员不存在");
        return ServerResponse.createBySuccess(userService.searchAllUsers(current, size));
    }

    @CrossOrigin
    @RequestMapping(value = "searchusersbyusername.do",method = RequestMethod.POST)
    public ServerResponse searchUsersByUsername( String username){
        int id = (int) super.getSession().getAttribute("admininfo");
      //int id = 1;
        Admin admin =new Admin();
        admin = adminService.getById(id);
        //username="a";
        User user=userService.findByUserName(username);
        if (admin == null) {
            return ServerResponse.createByErrorMsg("该管理员不存在");
        }
         else if (user == null) {
            return ServerResponse.createByErrorMsg("用户不存在");
        }
        return ServerResponse.createBySuccess(userService.findByUserName(username));
    }
}
